package com.baymax.springboot.cli.plugins.mybatis;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;

public class PaginationPlugin extends PluginAdapter {

    /**
     * 重写 validate 返回true，插件才能生效
     * @param list
     * @return
     */
    @Override
    public boolean validate(List<String> list) {
        return true;
    }

    /**
     * 为每个Example类添加limit和offset属性已经set、get方法
     */
    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
                                              IntrospectedTable introspectedTable) {

        List<IntrospectedColumn> allColumns = introspectedTable.getAllColumns();
        for(IntrospectedColumn column : allColumns) {
            System.out.println("column name is:" + column.getActualColumnName());
        }

        PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance()
                .getPrimitiveTypeWrapper();

        FullyQualifiedJavaType type = new FullyQualifiedJavaType(
                introspectedTable.getMyBatis3JavaMapperType());

        /**
         * 新增一个limit属性
         */
        Field limit = new Field("limit", type);
        limit.setVisibility(JavaVisibility.PRIVATE);
        limit.setType(integerWrapper);
        topLevelClass.addField(limit);

        /**
         * 新增一个setLimit方法
         */
        Method setLimit = new Method("setLimit");
        setLimit.setVisibility(JavaVisibility.PUBLIC);
        setLimit.addParameter(new Parameter(integerWrapper, "limit"));
        setLimit.addBodyLine("this.limit = limit;");
        topLevelClass.addMethod(setLimit);

        /**
         * 新增一个getLimit方法
         */
        Method getLimit = new Method("getLimit");
        getLimit.setVisibility(JavaVisibility.PUBLIC);
        getLimit.setReturnType(integerWrapper);
        getLimit.addBodyLine("return limit;");
        topLevelClass.addMethod(getLimit);

        /**
         * 新增一个offset属性
         */
        Field offset = new Field("offset", type);
        offset.setVisibility(JavaVisibility.PRIVATE);
        offset.setType(integerWrapper);
        topLevelClass.addField(offset);

        /**
         * 新增一个setOffset方法
         */
        Method setOffset = new Method("setOffset");
        setOffset.setVisibility(JavaVisibility.PUBLIC);
        setOffset.addParameter(new Parameter(integerWrapper, "offset"));
        setOffset.addBodyLine("this.offset = offset;");
        topLevelClass.addMethod(setOffset);

        /**
         * 新增一个getOffset方法
         */
        Method getOffset = new Method("getOffset");
        getOffset.setVisibility(JavaVisibility.PUBLIC);
        getOffset.setReturnType(integerWrapper);
        getOffset.addBodyLine("return offset;");
        topLevelClass.addMethod(getOffset);

        return true;
    }

    /**
     * 为 Mapper.xml 的 selectByExample 添加 limit
     */
    @Override
    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
                                                                     IntrospectedTable introspectedTable) {
        addPageElement(element);
        return true;
    }

    /**
     * 为 Mapper.xml 的 selectByExampleWithBLOBs 添加 limit
     */
    @Override
    public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element,
                                                                  IntrospectedTable introspectedTable) {
        addPageElement(element);
        return true;
    }

    /**
     * <if test="limit != null">
     *   <if test="offset != null">
     *     limit ${offset}, ${limit}
     *   </if>
     *   <if test="offset == null">
     *     limit ${limit}
     *   </if>
     * </if>
     * @param element
     */
    private void addPageElement(XmlElement element) {
        /**
         * 新增一个 <if test="limit != null">
         */
        XmlElement ifLimitNotNullElement = new XmlElement("if");
        ifLimitNotNullElement.addAttribute(new Attribute("test", "limit != null"));

        /**
         * 新增
         * <if test="offset != null">
         *   limit ${offset}, ${limit}
         * </if>
         */
        XmlElement ifOffsetNotNullElement = new XmlElement("if");
        ifOffsetNotNullElement.addAttribute(new Attribute("test", "offset != null"));
        ifOffsetNotNullElement.addElement(new TextElement("limit ${offset}, ${limit}"));
        ifLimitNotNullElement.addElement(ifOffsetNotNullElement);

        /**
         * 新增
         * <if test="offset == null">
         *   limit ${limit}
         * </if>
         */
        XmlElement ifOffsetNullElement = new XmlElement("if");
        ifOffsetNullElement.addAttribute(new Attribute("test", "offset == null"));
        ifOffsetNullElement.addElement(new TextElement("limit ${limit}"));
        ifLimitNotNullElement.addElement(ifOffsetNullElement);

        /*绑定到主上下文中以待生成文件*/
        element.addElement(ifLimitNotNullElement);
    }
}
